const Controller=require('egg').Controller;
let arr=[
    {name:'admin',pass:'123'}
]
class Jwt extends Controller{
    async jwtdata(){
        // let token = this.ctx.request.header.token;
        // try{
        //     let decode=this.app.jwt.verify(token,this.app.config.jwt.secret);
            this.ctx.body=arr
        // }catch{
        //     this.ctx.body='错误'
        // }
        
    }
    
    async jwtlogin(){
        // console.log(1);
        let user = this.ctx.request.body;
        console.log(user);
        if(user.name=='admin'&&user.pass=='123'){
            let token=this.app.jwt.sign(user.name,this.app.config.jwt.secret);
            this.ctx.body = {
                code:'200',
                token:token
            }
        }else{
            this.ctx.body = {
                code:'401',
                msg:'错误'
            }
        }
    }


    //示例
    async index(){
        this.ctx.body='hello jwt'
        let name={
            name:'momo'
        }
        //生成token
        let token=this.app.jwt.sign(name,this.app.config.jwt.secret);
        // this.ctx.body=token
        console.log(token);
        //校验token
        try{
            let decode=this.app.jwt.verify(token,this.app.config.jwt.secret)
            this.ctx.body=decode
        }catch(e){
            this.ctx.body='校验错误'
        }
        
    }
}
module.exports=Jwt;